<template>
  <div class="Safety">
    <van-nav-bar
      title="安全中心"
      left-text="返回"
      @click-left="back"
      fixed
    ></van-nav-bar>
    <div class="infoset">
      <van-cell
        :title="value.title"
        @click="myinfo(value.title)"
        v-for="(value, index) in infomains"
        :key="index"
      />
    </div>
    <van-button type="primary" @click="backinfo">退出登录</van-button>
  </div>
</template>

<script>
import { Dialog } from 'vant'
import { getToken, delToken } from '@/utils/storage'

import { safetyAPI } from '@/api/user.js'

export default {
  data () {
    return {
      infomains: [
        { title: '注销账号', name: '' },
        { title: '切换账号', name: '' },
        { title: '修改密码', name: '' }
      ]
    }
  },
  created () {
    const tokenString = getToken()
    if (!tokenString) return this.$router.push({ name: 'Login' })
  },
  methods: {
    back () {
      this.$router.push({ name: 'My' })
    },
    myinfo (value) {
      if (value === '注销账号') {
        const url = '/destroyAccount'
        Dialog.confirm({
          title: '注销账号',
          message: '确定注销账号吗?信息将会丢失!'
        }).then(() => {
          this.set(url)
        })
      }
      if (value === '修改密码') {
        this.$router.push({ name: 'setpasswo' })
      }
      if (value === '切换账号') {
        Dialog.confirm({
          title: '切换账号',
          message: '确定切换账号吗?'
        }).then(() => {
          delToken()
          this.$router.push({ name: 'Login' })
        })
      }
    },
    // 调用注销、退出登录账号api接口
    set (url) {
      const tokenString = getToken()
      safetyAPI(url, { appkey: this.$store.state.appkey, tokenString }).then(
        res => {
          console.log(res)
          if (res.code === 'G001' || res.code === 'F001') {
            // 退出登录删除token返回登录页面
            delToken()
            this.$router.push({ name: 'Login' })
          }
          this.$toast(res.msg)
        }
      )
    },
    // 退出登录
    backinfo () {
      const url = '/logout'
      Dialog.confirm({
        title: '退出登录',
        message: '确定退出登录吗?'
      }).then(() => {
        this.set(url)
      })
    }
  }
}
</script>
<style lang="less" scoped>
.Safety {
  padding: 50px 10px 0;
  /deep/ .van-cell {
    margin-bottom: 10px;
    background-color: #182094;
    color: white;
  }
  /deep/ .van-button {
    left: 10%;
    width: 80%;
    background-color: #ee0a24;
    border-color: #ee0a24;
    border-radius: 20px;
    margin-top: 100px;
  }
}
</style>
